package com.jxc.jxc01.hch.web;

import com.alibaba.fastjson2.JSONObject;
import com.jxc.jxc01.hch.entity.RespResult;
import com.jxc.jxc01.hch.entity.User;
import com.jxc.jxc01.hch.service.ILoginService;
import com.jxc.jxc01.hch.service.Impl.ILoginServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

@WebServlet("*.login")
public class LoginServlet extends HttpServlet {
    ILoginService loginService = new ILoginServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String uri = req.getRequestURI();
        System.out.println(uri);
        switch (uri){
            case "/jxc01/verify.login":
                verify(req,resp);
            case "/jxc01/login.login":
                login(req,resp);
        }

    }

    /**
     * 用户登录验证
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
//            //判断验证码是否一致
//            HttpSession session = req.getSession();
//            String sCode = (String) session.getAttribute("code");
//            //获取验证码
//            String rCode = req.getParameter("code");
//            if (null==sCode || null==rCode){
//                out.print(JSONObject.toJSONString(RespResult.fail()));
//                return;
//            }
//            if(!rCode.equals(sCode)){
//                out.print(JSONObject.toJSONString(RespResult.fail()));
//                return;
//            }
            //获取账号密码
            String username =  req.getParameter("username");
            String password = req.getParameter("password");
            ILoginService loginService = new ILoginServiceImpl();
            User user = loginService.login(username,password);
            System.out.println(user);
            if (null==user.getUsername()){
                out.print("您输入的账号或密码不正确");
                return;
            }
            HttpSession session = req.getSession();
            //session = req.getSession();
            session.setAttribute("user",user);
            resp.sendRedirect("index.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void verify(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            //获取用户手机号
            String phone = req.getParameter("phone");
            System.out.println(phone);
            String  code = loginService.verify(phone);
            HttpSession session =req.getSession();
            session.setAttribute("code",code);
            out.print(RespResult.success());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


}
